package com.fubowen.lepton.ui.base

import android.graphics.Color
import android.graphics.Rect
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.PopupWindow
import androidx.core.view.doOnLayout
import androidx.core.view.doOnPreDraw
import androidx.fragment.app.FragmentManager
import com.fubowen.lepton.R

open class ThemePopupWindowFragment : ThemeFragment() {

    private lateinit var popup: PopupWindow
    open var mIsOutsideTouchable = true
    open var mIsTouchable = true
    open var mIsFocusable = true
    open var mAnimationStyle = R.style.PopupAnimation
    open var mElevation = 0f
    open var mOverlapAnchor = true

    fun getPopup() = popup

    //region onViewCreated
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        popup = PopupWindow().apply {
            contentView = view
            width = ViewGroup.LayoutParams.MATCH_PARENT
            height = ViewGroup.LayoutParams.WRAP_CONTENT
            isOutsideTouchable = mIsOutsideTouchable
            isTouchable = mIsTouchable
            isFocusable = mIsFocusable
            overlapAnchor = mOverlapAnchor
            isClippingEnabled = true
            animationStyle = mAnimationStyle
            elevation = mElevation
        }
        super.onViewCreated(view, savedInstanceState)
    }
    //endregion

    //region measurePopupWindow
    private fun measurePopupWindow(): Int {

        val sw = requireContext().resources.displayMetrics.widthPixels
        if (popup.height > 0) {
            return popup.height
        }
        val w = popup.width
        val measureWidthParams = when {
            w > 0 -> {
                View.MeasureSpec.makeMeasureSpec(w, View.MeasureSpec.EXACTLY)
            }

            w == WindowManager.LayoutParams.MATCH_PARENT -> {
                View.MeasureSpec.makeMeasureSpec(sw, View.MeasureSpec.EXACTLY)
            }

            w == WindowManager.LayoutParams.WRAP_CONTENT -> {
                popup.width = sw / 2
                View.MeasureSpec.makeMeasureSpec(popup.width, View.MeasureSpec.EXACTLY)
            }

            else -> 0
        }
        popup.contentView.measure(
            measureWidthParams,
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
        )
        return popup.contentView.measuredHeight
    }

    //endregion
    fun show(fm: FragmentManager, tag: String, anchor: View) {
        if (!this.isAdded) {
            val ft = fm.beginTransaction()
            ft.setReorderingAllowed(true)
            ft.add(this, tag)
            ft.commitNow()
        }
        val height = measurePopupWindow()
        popup.contentView.translationY
        popup.contentView. addOnLayoutChangeListener { v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom ->
            val dd = v.translationY
            Log.i("重绘", "$dd ")
        }
        popup.showAsDropDown(anchor, 0, -height)
    }
}